<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品类别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入商品类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >下单
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column label="用户名称" align="center" prop="username" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品类别" align="center" prop="category" />
      <el-table-column label="商品数量" align="center" prop="num" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBuy(scope.row)"

          >下单
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 购物车下单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <!-- 商品信息部分 -->
      <el-row>
        <el-col :span="12">
          <el-text style="font-weight: bold;">商品名：</el-text>
          <el-text>{{ form.goodsName }}</el-text>
        </el-col>
        <el-col :span="12">
          <el-text style="font-weight: bold;">商品数量：</el-text>
          <el-text>{{ form.num }}</el-text>
        </el-col>
      </el-row>

      <!-- 分割线 -->
      <el-divider></el-divider>

      <!-- 表单部分 -->
      <el-form ref="dealForm" :model="deal" :rules="dealRules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="收货地址" prop="addressId">
              <el-select v-model="deal.addressId" placeholder="请选择收货地址" style="width: 100%">
                <el-option
                  v-for="item in addressList"
                  :key="item.id"
                  :label="item.province + ' ' + item.city + ' ' + item.district + ' - 收件人：' + item.consignee + ' 电话：' + item.phone"
                  :value="item.id">
                  <span style="float: left">{{ item.province + ' ' + item.city + ' ' + item.district }}</span>
                  <span style="float: right; font-size: 13px">收件人：{{ item.consignee }} 电话：{{ item.phone }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-radio-group v-model="deal.paymentMethod">
                <el-radio :label="3">微信支付</el-radio>
                <el-radio :label="6">支付宝支付</el-radio>
                <el-radio :label="9">银联支付</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 底部操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDeal">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCar, delCar, getCar, listCar, updateCar, removeAfterOrder } from '@/api/client/car'
import { listAddress } from '@/api/client/address'
import { addDeal } from '@/api/client/deal'

export default {
  name: 'Car',
  dicts: ['sys_car_status', 'sys_car_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物车表格数据
      carList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carTitle: [
          { required: true, message: '购物车标题不能为空', trigger: 'blur' }
        ],
        carType: [
          { required: true, message: '购物车类型不能为空', trigger: 'change' }
        ]
      },
      // 收货地址列表
      addressList: [],

      // 订单表单
      deal: {
        addressId: undefined,
        goodsId: undefined,
        count: 0,
        paymentMethod: 3
      },

      // 订单表单校验规则
      dealRules: {
        addressId: [
          { required: true, message: '请选择收货地址', trigger: 'change' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getAddressList()
  },
  methods: {
    /** 查询购物车列表 */
    getList() {
      this.loading = true
      listCar(this.queryParams).then(response => {
        this.carList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        carId: undefined,
        carTitle: undefined,
        carType: undefined,
        carContent: undefined,
        status: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加购物车'
    },
    /** 获取收货地址列表 */
    getAddressList() {
      listAddress().then(response => {
        this.addressList = response.data
      })
    },
    /** 下单按钮操作 */
    handleBuy(row) {
      this.reset()
      const carId = row.id || this.ids
      getCar(carId).then(response => {
        this.form = response.data
        // 初始化订单数据
        this.deal.goodsId = this.form.goodsId
        this.deal.count = this.form.num
        this.open = true
        this.title = '购物车下单'
      })
    },
    /** 提交订单 */
    submitDeal() {
      this.$refs['dealForm'].validate(valid => {
        if (valid) {
          addDeal(this.deal).then(response => {
            // 下单成功后删除购物车数据
            removeAfterOrder(this.form.id).then(() => {
              this.$modal.msgSuccess('下单成功')
              this.open = false
              this.getList()
            })
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.id || this.ids
      this.$modal.confirm('是否确认删除购物车编号为"' + carIds + '"的数据项？').then(function() {
        return delCar(carIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
